package cn.example.vip.mall.goods.service.impl;

import cn.example.vip.mall.goods.entity.CategoryBrand;
import cn.example.vip.mall.goods.mapper.CategoryBrandMapper;
import cn.example.vip.mall.goods.service.CategoryBrandService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements CategoryBrandService {
	@Autowired
	CategoryBrandMapper categoryBrandMapper;
	
	/**
	 * 根据 分类id 查询 品牌
	 * @param categoryBrand
	 * @return
	 */
	@Override
	public List<CategoryBrand> queryListByCategory(CategoryBrand categoryBrand) {
		QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper();
		queryWrapper.eq(categoryBrand.getCategoryId() != null, "category_id", categoryBrand.getCategoryId());
		return categoryBrandMapper.selectList(queryWrapper);
	}
	
	/**
	 * 根据 分类id 分页查询 品牌
	 * @param categoryBrand
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public Page<CategoryBrand> queryPageListByCategory(CategoryBrand categoryBrand, Long pageNum, Long pageSize) {
		QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper();
		queryWrapper.eq(categoryBrand.getCategoryId() != null, "category_id", categoryBrand.getCategoryId());
		return categoryBrandMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
	}
	
	/**
	 * 根据 品牌id 查找 分类
	 * @param categoryBrand
	 * @return
	 */
	@Override
	public List<CategoryBrand> queryListByBrand(CategoryBrand categoryBrand) {
		QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper();
		queryWrapper.eq(categoryBrand.getBrandId() != null, "brand_id", categoryBrand.getBrandId());
		return categoryBrandMapper.selectList(queryWrapper);
	}
	
	/**
	 * 根据 品牌id 分页查找 分类
	 * @param categoryBrand
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@Override
	public Page<CategoryBrand> queryPageListByBrand(CategoryBrand categoryBrand, Long pageNum, Long pageSize) {
		QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper();
		queryWrapper.eq(categoryBrand.getBrandId() != null, "brand_id", categoryBrand.getBrandId());
		return categoryBrandMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
	}
	
	/**
	 * 查找 键值对 并更新
	 * @param categoryBrand
	 * @return
	 */
	@Override
	public Integer updateByCategoryBrand(CategoryBrand categoryBrand, Integer categoryId, Integer brandId) {
		QueryWrapper<CategoryBrand> queryWrapper = new QueryWrapper();
		if (categoryBrand.getCategoryId() == null) {
			throw new RuntimeException("updateByCategoryBrand: 'CategoryId' is null");
		}
		if(categoryBrand.getBrandId() == null) {
			throw new RuntimeException("updateByCategoryBrand: 'BrandId' is null");
		}
		queryWrapper.eq("category_id", categoryId);
		queryWrapper.eq("brand_id", brandId);
		return categoryBrandMapper.update(categoryBrand, queryWrapper);
	}
	
	@Override
	public Integer deleteByCategory(CategoryBrand categoryBrand) {
		QueryWrapper<CategoryBrand> wrapper = new QueryWrapper<>();
		wrapper.eq("category_id", categoryBrand.getCategoryId());
		wrapper.eq("brand_id", categoryBrand.getBrandId());
		return categoryBrandMapper.delete(wrapper);
	}
	
}
